package controller

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisService"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeTSQL"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
)

type deviceSequenceEntityManageController struct {
}

func (e *deviceSequenceEntityManageController) init() {
	httpserver.Server.POST("/device/sequence/record/page", e.selectEntityPage)
}

func (e *deviceSequenceEntityManageController) selectEntityPage(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	entityList, err := edgeTSQL.QueryRecords(body)
	if err != nil {
		return nil, err
	}

	mapList := edgeTSQL.BuildMapList(entityList)

	e.extend(mapList)

	data := make(map[string]interface{})
	data["list"] = mapList
	data["total"] = 1000

	return data, nil
}

func (e *deviceSequenceEntityManageController) extend(mapList []map[string]interface{}) {
	for _, data := range mapList {
		deviceName := Map.GetString(data, "deviceName", "")

		deviceEntity := commRedisService.GetDeviceEntity(deviceName)
		if deviceEntity == nil {
			continue
		}

		data["manufacturer"] = deviceEntity.Manufacturer
		data["deviceType"] = deviceEntity.DeviceType
	}
}
